#!/usr/bin/perl
if(@ARGV != 1){
        die "WARNNING: The program needs a 3-way merged file.\n";
}
$msg1 ="\033[0m\033[1;32m##############################BEGIN:\n".
	   "###This part is only for notification,\n".
	   "###and should be removed in final result!\n".
	   "###TRUNK's MODIFICATION:\n\033[0m";
$msg2 = "\033[0m\033[1;32m###BRANCH's MODIFICATION:\n\033[0m";
$msg3 = "\033[0m\033[1;32m###MERGED MODIFICATION:\n\033[0m";
$msg4=  "\033[0m\033[1;32m##############################END:\n\033[0m";
$fileName = $ARGV[0];
open MERGE, "$fileName";
open OTMERGE, ">$fileName.ot";
@lines = <MERGE>;
for ($i=0;$i<=$#lines;$i++){
	$_ = $lines[$i];
	if(/^<<<<<<< .*\.base$/){
		$i++;
		while(1){
			if($i <= $#lines){
				$_ = $lines[$i];
				if(/^=======/){
					$i++;
					last;
				}else
				{
					$i++;
				}
			}
		}
		while(1){
			if($i <= $#lines){
				$_ = $lines[$i];
				if(/^>>>>>>>/){
					$i++;
					last;
				}else
				{
					if($i <= $#lines){
						$_ = $lines[$i];
					}
					print  "\033[1;34m";
					print OTMERGE;
					print;
					print  "\033[0;0m";
					$i++;
				}
			}
		}
	}else{
		$_ = $lines[$i];
		if(/^<<<<<<< .*\.trunk$/)
		{
			open TMPBASE, ">tmp.base";
			open TMPTRUNK, ">tmp.trunk";
			open TMPBRANCH, ">tmp.branch";
			$i++;
			while(1){
				if($i <= $#lines){
					$_ = $lines[$i];
					if(/^\|\|\|\|\|\|\|/){
						$i++;
						last;
					}else
					{
						if($i <= $#lines){
							$_ = $lines[$i];
						}
						if(defined($_)){
							print TMPTRUNK;
						}
						$i++;
					}
				}
			}
			while(1){
				if($i <= $#lines){
					$_ = $lines[$i];
					if(/^=======/){
						$i++;
						last;
					}else
					{
						if($i <= $#lines){
							$_ = $lines[$i];
						}
						if(defined($_)){
							print TMPBASE;
						}
						$i++;
					}
				}
			}
			while(1){
				if($i <= $#lines){
					$_ = $lines[$i];
					if(/^>>>>>>>/){
#						$i++;
						last;
					}else
					{
						if($i <= $#lines){
							$_ = $lines[$i];
						}
						if( defined($_)){
							print TMPBRANCH;
						}
						$i++;
					}
				}
			}
			close TMPTRUNK;
			close TMPBRANCH;
			close TMPBASE;
			$ot_merge_result = `merge.pl tmp.base tmp.trunk tmp.branch`;
			$eds_trunk = `ed.pl tmp.base tmp.trunk`;
			$trunk_modification = `colorEDS.pl tmp.base $eds_trunk`;
			$eds_branch = `ed.pl tmp.base tmp.branch`;
			$branch_modification = `colorEDS.pl tmp.base $eds_branch`;
			##########Print to STDOUT
			print $msg1;
			print $trunk_modification;
			print $msg2;
			print $branch_modification;
			print $msg3;
			#########Print to File
			$msg1_file = $msg1;
			$trunk_modification_file = $trunk_modification;
			$msg2_file = $msg2;
			$branch_modification_file = $branch_modification;
			$msg3_file = $msg3;
			$msg4_file = $msg4;

			$trunk_modification_file =~ s/\033\[0m\033\[5m\033\[4m.\033\[0m//sg;
			$branch_modification_file =~ s/\033\[0m\033\[5m\033\[4m.\033\[0m//sg;	
			
			$msg1_file =~ s/\033.*?\d+m//g;
			$trunk_modification_file =~ s/\033.*?\d+m//g;
			$msg2_file =~ s/\033.*?\d+m//g;
			$branch_modification_file =~ s/\033.*?\d+m//g;
			$msg3_file =~ s/\033.*?\d+m//g;
			$msg4_file =~ s/\033.*?\d+m//g;
			
			print OTMERGE $msg1_file;
			print OTMERGE $trunk_modification_file;
			print OTMERGE $msg2_file;
			print OTMERGE $branch_modification_file;
			print OTMERGE $msg3_file;
			############
			system("rm -fr tmp.*");
			print  "\033[1;36m";
			chomp($ot_merge_result);
			print OTMERGE $ot_merge_result."\n";
			print $ot_merge_result."\n";
			print  "\033[0;0m";
			print $msg4;
			print OTMERGE $msg4_file;
		}else{
			print OTMERGE;
			print;
		}
	}
}
